<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <el-form-item label="备品备件分类" prop="spareTypeCode">
        <treeselect
          v-model="formItem.spareTypeCode"
          :multiple="false"
          :options="typeOptions"
          placeholder="选择备品备件类型"
          @select="treeSelected"
        />
      </el-form-item>
      <el-form-item label="货品名称" prop="goodsFileId">
        <el-select v-model="formItem.goodsFileId" placeholder="请选择货品" @change="onSelect">
          <el-option v-for="(item) in goodsFile" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="规格型号" prop="specification">
        <el-input v-model="formItem.specification" disabled />
      </el-form-item>
      <el-form-item label="数量" required prop="number">
        <el-input v-model="formItem.number" placeholder="请输入数量" />
      </el-form-item>
      <el-form-item label="存放库房" required prop="storehouseId">
        <el-select v-model="formItem.storehouseId" placeholder="请选择仓库">
          <el-option v-for="(item) in storehouse" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="批次" required prop="batch">
        <el-input v-model="formItem.batch" placeholder="请输入批次名称" />
      </el-form-item>
      <el-form-item label="供应商" required prop="manufacturerId">
        <el-select v-model="formItem.manufacturerId" placeholder="请选择供应商">
          <el-option v-for="(item) in manufacturer" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input-number v-model="formItem.unitPrice" :precision="2" :step="1" :max="1000000000" />
      </el-form-item>
      <el-form-item label="应用资产分类" prop="applyAssetTypeName">
        <el-input v-model="formItem.applyAssetTypeName" disabled />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  update,
  detail
} from '@neu/dcam-ui/api/stock'
import {
  sptList as goodsFileList, detail as gfDetail } from '@neu/dcam-ui/api/goodsFile'
import { list as storehouseList } from '@neu/dcam-ui/api/storehouse'
import { list as manufacturerList } from '@neu/dcam-ui/api/manufacturer'

import {
  tree
} from '@neu/dcam-ui/api/spareType'

// 定义一个表单原始项
const formItem = {
  spareTypeCode: null,
  goodsFileId: '',
  goodsFileName: '',
  specification: '',
  batch: '',
  number: '',
  unitPrice: '',
  applyAssetTypeName: '',
  manufacturerId: '',
  manufacturerName: '',
  storehouseId: '',
  storehouseName: ''
}
export default {
  name: 'StockEditForm',
  components: {

  },
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      /** 货品信息 */
      goodsFile: [],
      /** 供应商 */
      manufacturer: [],
      /** 仓库 */
      storehouse: [],
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        goodsFileId: [{ required: true, message: '货品不能为空', trigger: 'blur' }],
        number: [
          { required: true, message: '数量', trigger: 'change' },
          {
            validator: function(rule, value, callback) {
              if (/^[0-9]*$/.test(value) === false) {
                callback(new Error('请输入正确的数字'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }],
        batch: [{ required: true, message: '批次不能为空', trigger: 'blur' },
          { max: 100, message: '长度不能超过100', trigger: 'blur' }],
        manufacturerId: [{ required: true, message: '选择类型供应商', trigger: 'change' }],
        storehouseId: [{ required: true, message: '选择类型仓库', trigger: 'change' }],
        spareTypeCode: [
          { required: true, message: '备品备件分类不能为空', trigger: 'change' }
        ]
      },
      modal: true,
      /** 备件类型树 */
      typeOptions: []
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
    manufacturerList().then(response => {
      this.manufacturer = response.data
    })
    storehouseList().then(response => {
      this.storehouse = response.data
    })

    this.tree()
  },
  methods: {
    treeSelected(node) {
      this.formItem.goodsFileId = ''
      this.formItem.goodsFileName = ''
      this.formItem.specification = ''
      this.formItem.applyAssetTypeName = ''
      goodsFileList(node.code).then(response => {
        this.goodsFile = response.data
      })
    },
    onSelect(id) {
      gfDetail(id).then(response => {
        const data = response.data
        this.formItem.specification = data.specification
        this.formItem.applyAssetTypeName = data.parentAssetTypeName
      })
    },
    // 类型树
    tree() {
      tree().then(response => {
        this.typeOptions = response.data
      })
    },
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          goodsFileList(response.data.spareTypeCode).then(response => {
            this.goodsFile = response.data
          })
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }

  }
}
</script>
